<template>
<div class="list" ref="wrapper">
  <div>
    <div class="area">
      <div class="title">当前城市</div>
      <div class="button-list">
        <div class="button-wrapper">
          <div class="button">{{this.$store.state.city}}</div>
        </div>
      </div>
    </div>
    <div class="area">
      <div class="title">热门城市</div>
      <div class="button-list">
        <div class="button-wrapper" v-for="item of hot" :key="item.id" @click="handleCityClick(item.name)">
          <div class="button">{{item.name}}</div>
        </div>
      </div>
    </div>
    <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
      <div class="title">{{key}}</div>
      <div class="item-list">
        <div class="item" v-for="innerItem of item" :key="innerItem.id" @click="handleCityClick(innerItem.name)">{{innerItem.name}}</div>
      </div>
    </div>
  </div>
</div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import BScroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    handleCityClick (city) {
      this.changeCity(city)
      this.$router.push('/')// 点击完直接跳转首页
    },
    ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true
    })
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style scoped lang="stylus">
@import '~styles/varibles.styl'
.list {
  overflow:hidden
  position :absolute
  top:4.53rem
  left:.5rem
  right:.5rem
  bottom:.5rem
}
  .title {
    line-height:1.5rem
    background: #eee
    padding-left:.2rem
    color: #666
    border-top:1px solid #ccc
    border-bottom:1px solid #ccc
  }
  .button-list {
    overflow:hidden
    padding:.2rem 1.2rem .2rem .2rem
  }
  .button-wrapper {
    float:left
    width:33.33%
  }
  .button {
    padding :.1rem 0sss
    text-align:center
    border:1px solid #ccc
    margin:.2rem
    border-radius :.1rem
  }
  .item {
    padding-left:.2rem
    line-height:1.7rem
    border-bottom:1px solid #e5e5e5
  }
</style>
